package com.ma.controller;

import com.alibaba.fastjson.JSON;
import com.ma.dto.OrderQueryParam;
import com.ma.pojo.Order;
import com.ma.pojo.OrderDetails;
import com.ma.service.order.OrderService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.*;
/**
*
*
@Author: xiaoma
@Date: 10:01 2020/8/26
@Description: 订单有关CRUD
*/

@Controller
@CrossOrigin
@RequestMapping("/order")
public class OrderController {
    private OrderService orderService;

    public OrderController(OrderService orderService) {
        this.orderService = orderService;
    }
    @RequestMapping("/Count")
    @CrossOrigin
    @ResponseBody
    public Object orderCount(){
        //用于main页面所需的数据
        Integer allOrderCount = orderService.queryAllOrder();
        //查询总金额
        float totalPrice = orderService.queryTotalPrice();
        System.out.println(totalPrice);
        HashMap<String,Object> map = new HashMap<>();
        map.put("orderCount",allOrderCount);
        //将总金额放入map
        map.put("totalPrice",totalPrice);
        System.out.println(allOrderCount);
        JSON json = (JSON) JSON.toJSON(map);
        return json;
    }

    @RequestMapping("/getOrder")
    public String getOrder(Model model){
        System.out.println("========getOrder=======");
        List<Order> list = orderService.queryAll();
        System.out.println(list);
        model.addAttribute("list",list);

        return "order";
    }

//    @RequestMapping("/queryOrder")
//    public String queryOrder(String orderId, String orderDate,String customerId, String payWay,Model model){
//        System.out.println("=======queryOrder========");
//        System.out.println(customerId);
//        HashMap<String, String> map = new HashMap<>();
//        if (orderId != null){
//            map.put("orderId",orderId);
//            String orderID = orderService.queryOrder(map);
//            Order order = orderService.queryByOrderId(orderID);
//            List<Order> list = new ArrayList<>();
//            list.add(order);
//            model.addAttribute("list",list);
//        }else if (orderDate != null){
//            System.out.println(orderDate);
//
//        } else if (customerId != null){
//            map.put("customerId",customerId);
//            System.out.println(customerId);
//            String orderID = orderService.queryOrder(map);
//            Order order = orderService.queryByOrderId(orderID);
//            List<Order> list = new ArrayList<>();
//            list.add(order);
//            model.addAttribute("list",list);
//        }else if (payWay != null) {
//            map.put("payWay", payWay);
//            String orderID = orderService.queryOrder(map);
//            Order order = orderService.queryByOrderId(orderID);
//            List<Order> list = new ArrayList<>();
//            list.add(order);
//            model.addAttribute("list",list);
//        } else {
//            List<Order> list = new ArrayList<>();
//            list = orderService.queryAll();
//            String msg = new String("无查询结果");
//            Model model1 = model.addAttribute("msg", msg);
//        }
//
//        return "order";
//    }
    @RequestMapping("/queryOrder")
    public String queryOrder(String orderId,Model model){
        System.out.println("=======queryOrder========");
        Order order1 = orderService.queryByOrderId(orderId);
        List<Order> list = new ArrayList<>();
        list.add(order1);
        System.out.println(order1);
        if(order1 == null){
            list = orderService.queryAll();
            String msg = new String("无查询结果");
            Model model1 = model.addAttribute("msg", msg);
            System.out.println(model);
        }
        model.addAttribute("list",list);
        return "order";
    }

    @RequestMapping("/toOrderTail")
    public String toOrderTail(String id,Model model){
        System.out.println("========toOrderTail=======");
        String orderDetailsId = orderService.queryOrderDetailsById(id);
        OrderDetails orderDetails = orderService.queryOrderDetails(orderDetailsId);
        model.addAttribute("orderDetails",orderDetails);
        return "orderDetails";
    }
    @CrossOrigin
    @RequestMapping(value = "/list",method = RequestMethod.POST)
    @ResponseBody
    public Object list(OrderQueryParam orderQueryParam){
        System.out.println("========List=======");
        List<Order> orderList = orderService.list(orderQueryParam);
        System.out.println(orderList);
        JSON json = (JSON) JSON.toJSON(orderList);
        return json;
    }
}
